package com.yanwq.nwa.im;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class PingService extends Service {
    private boolean pinging;

    public PingService() {
        pinging = false;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (pinging) {
            Log.d("PingService", "ping has been started!");
        } else {
            startPing();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void startPing() {
        Intent intent = new Intent("Ping");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0xf1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10 * 1000, 60 * 1000, pendingIntent);
        pinging = true;
    }
}